package com.ruoyi.project.yingyu.activity.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MoneyUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.yingyu.activity.domain.Activity;
import com.ruoyi.project.yingyu.activity.mapper.ActivityMapper;
import com.ruoyi.project.yingyu.activity.service.IActivityService;
import com.ruoyi.project.yingyu.activityReg.mapper.ActivityRegMapper;

/**
 * 活动Service业务层处理
 * 
 * @author shixiangwen
 * @date 2019-11-29
 */
@Service
public class ActivityServiceImpl implements IActivityService {

	@Autowired
	private ActivityMapper activityMapper;

	@Autowired
	private ActivityRegMapper activityRegMapper;

	/**
	 * 查询活动
	 * 
	 * @param id 活动ID
	 * @return 活动
	 */
	@Override
	public Activity selectActivityById(Integer id) {
		return activityMapper.selectActivityById(id);
	}

	/**
	 * 查询活动列表
	 * 
	 * @param activity 活动
	 * @return 活动
	 */
	@Override
	public List<Activity> selectActivityList(Activity activity) {
		List<Activity> list = activityMapper.selectActivityList(activity);
		list.forEach(item -> {
			item.setRegYuan(MoneyUtil.fee2yuan(item.getRegFee()).toString());
			item.setDiscountRegYuan(MoneyUtil.fee2yuan(item.getDiscountRegFee()).toString());
			int regCount = activityRegMapper.selectActivityRegCount(item.getId(), 1);
			item.setRegCount(regCount);
		});
		return list;
	}

	/**
	 * 新增活动
	 * 
	 * @param activity 活动
	 * @return 结果
	 */
	@Override
	public int insertActivity(Activity activity) {

		Date regStartTime = DateUtils.parseDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, activity.getRegStartTime()) + " 00:00:00");
		Date regEndTime = DateUtils.parseDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, activity.getRegEndTime()) + " 23:59:59");
		activity.setRegStartTime(regStartTime);
		activity.setRegEndTime(regEndTime);

		return activityMapper.insertActivity(activity);
	}

	/**
	 * 修改活动
	 * 
	 * @param activity 活动
	 * @return 结果
	 */
	@Override
	public int updateActivity(Activity activity) {

		Date regStartTime = DateUtils.parseDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, activity.getRegStartTime()) + " 00:00:00");
		Date regEndTime = DateUtils.parseDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, activity.getRegEndTime()) + " 23:59:59");
		activity.setRegStartTime(regStartTime);
		activity.setRegEndTime(regEndTime);

		return activityMapper.updateActivity(activity);
	}

	/**
	 * 删除活动对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteActivityByIds(String ids) {
		return activityMapper.deleteActivityByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除活动信息
	 * 
	 * @param id 活动ID
	 * @return 结果
	 */
	@Override
	public int deleteActivityById(Integer id) {
		Activity activity = activityMapper.selectActivityById(id);
		if (activity.getStatus() != 0) {
			throw new BusinessException("活动已经开始，不能删除！");
		}
		return activityMapper.deleteActivityById(id);
	}
}
